package dyyx.drools.simple;

import java.io.StringReader;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatelessKnowledgeSession;

import dyyx.Utils;

public class SimpleMain2 {

	public static void main(String[] args) throws Exception {
		
		while(true){
			run();
		}

	}

	private static void run() throws Exception {

		String drl = Utils.readFromClassPath("drools/simple.drl", null);

		long start = System.currentTimeMillis();
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
		Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
		kbase.addKnowledgePackages(pkgs);
		long end = System.currentTimeMillis();
		long time = end - start;
		System.out.println("time=" + time);

		StatelessKnowledgeSession session = kbase.newStatelessKnowledgeSession();
		// ScoreHandler scoreHandler = new ScoreHandler();
		// session.setGlobal("score_handler", scoreHandler);

		Map context = new HashMap();
		context.put("age", 20);

		session.execute(context);
		System.out.println(context);

		context = new HashMap();
		context.put("age", 9);
		session.execute(context);
		System.out.println(context);

	}
}